const app = getApp()
const db = wx.cloud.database()
Page({
  data: {
    keyword: "",
    sets: null,
    isLoading: false,
    firstRefresh: false,
    page: 0,
  }, refresh(back) {
    wx.cloud.callFunction({ name: 'function', data: { type: "getPost", page: 0, where: {content: db.RegExp({regexp:this.data.keyword,options:'i'})}}}).then(res => {
      this.setData({ page: 0, firstRefresh: true })
      this.setData({ sets: res.result.user.data })
      if(back){back(res)}
    })
  }, onLoad(options) {
    this.setData({ keyword: options.word })
    wx.setNavigationBarTitle({ title: "'"+options.word+"'的搜索结果" })
    this.refresh()
  },
  onPageScroll(a) {
    if (a.scrollTop > this.data.toolTop) {
      this.setData({ toolbar: "position:fixed" })
    } else {
      this.setData({ toolbar: "" })
    }
    this.setData({ toolbar: this.data.toolbar })
  },onPullDownRefresh: function () {
    this.refresh(function (res) {
      wx.stopPullDownRefresh()
    })
  }, onReachBottom() {
    if ((!this.data.isLoading) && this.data.firstRefresh){
      this.setData({ isLoading: true, page: this.data.page + 1 })
      wx.cloud.callFunction({name:'function',data:{type: "getPost",page:this.data.page,where:{
        content:db.RegExp({regexp:this.data.keyword,options:'i'})
      } } }).then(res => {
        if (res.result.user.data.length < 10) { this.setData({ page: this.data.page }) }
        var set = this.data.sets
        this.setData({ sets: set.concat(res.result.user.data) })
        this.setData({ isLoading: false })
      })
    }
  }
})